<template>
	<view>
		<view>
			<navTop title="上传图片" :isHeight="false" hasBgc="white" :color="navtop.color" :isBack="true"></navTop>
		</view>
		<view class="upload">
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10">
			</u-upload>
		</view>
	</view>
</template>

<script>
	import navTop from '../../components/navTop.vue'
	export default {
		components: {
			navTop,

		},
		data() {
			return {
				navtop: {
					title: 'XXX',
					color: '#000000'
				},
				fileList1: [],
				id:'',
			}
		},
		onLoad(val) {
			var data=JSON.parse(val.content)
			this.id=data.id;
			
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that=this
				return new Promise((resolve, reject) => {
					uni.compressImage({
					  src: url,
					  quality: 10,
					  success: res => {
					   
						let a = uni.uploadFile({
							url: 'https://www.codingyt.com/club/images/insert ', // 仅为示例，非真实的接口地址
							filePath: res.tempFilePath,
							name: 'img',
							formData: {
							getId:that.id,
							getType:'association',
						},
							success: (res) => {
								setTimeout(() => {
									resolve(res.data.data)
								}, 1000)
							},
						});
					  }
					})
				})
			},
		},

	}
</script>

<style>
	.upload {
		position: absolute; 
		top: 200rpx;
	}
</style>
